<template>
  <el-dialog
    :title="!dataForm.cid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="品牌名称" prop="cName">
      <el-input v-model="dataForm.cName" placeholder="品牌名称"></el-input>
    </el-form-item>
    <el-form-item label="剩余数量" prop="cRemain">
      <el-input v-model="dataForm.cRemain" placeholder="剩余数量"></el-input>
    </el-form-item>
    <el-form-item label="租金,单位:元/小时" prop="cRent">
      <el-input v-model="dataForm.cRent" placeholder="租金,单位:元/小时"></el-input>
    </el-form-item>
    <el-form-item label="押金" prop="cCash">
      <el-input v-model="dataForm.cCash" placeholder="押金"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="cCreateTime">
      <el-input v-model="dataForm.cCreateTime" placeholder="创建时间(格式:2020-02-02 00:00:00)"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="cUpdateTime">
      <el-input v-model="dataForm.cUpdateTime" placeholder="更新时间(格式:2020-02-02 00:00:00)"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="cComment">
      <el-input v-model="dataForm.cComment" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="电单车采购单价" prop="cPrice">
      <el-input v-model="dataForm.cPrice" placeholder="单车采购单价"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          cid: 0,
          cName: '',
          cRemain: '',
          cRent: '',
          cCash: '',
          cCreateTime: '',
          cUpdateTime: '',
          cComment: '',
          cPrice: ''
        },
        dataRule: {
          cName: [
            { required: true, message: '品牌名称不能为空', trigger: 'blur' }
          ],
          cRemain: [
            { required: true, message: '剩余数量不能为空', trigger: 'blur' }
          ],
          cRent: [
            { required: true, message: '租金,单位:元/小时不能为空', trigger: 'blur' }
          ],
          cCash: [
            { required: true, message: '押金不能为空', trigger: 'blur' }
          ],
          cCreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          cUpdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          cComment: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          cPrice: [
            { required: true, message: '单车采购单价不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.cid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.cid) {
            this.$http({
              url: this.$http.adornUrl(`/bike/category/info/${this.dataForm.cid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.cName = data.category.cname
                this.dataForm.cRemain = data.category.cremain
                this.dataForm.cRent = data.category.crent
                this.dataForm.cCash = data.category.ccash
                this.dataForm.cCreateTime = data.category.ccreateTime
                this.dataForm.cUpdateTime = data.category.cupdateTime
                this.dataForm.cComment = data.category.ccomment
                this.dataForm.cPrice = data.category.cprice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bike/category/${!this.dataForm.cid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'cid': this.dataForm.cid || undefined,
                'cname': this.dataForm.cName,
                'cremain': this.dataForm.cRemain,
                'crent': this.dataForm.cRent,
                'ccash': this.dataForm.cCash,
                'ccreateTime': this.dataForm.cCreateTime,
                'cupdateTime': this.dataForm.cUpdateTime,
                'ccomment': this.dataForm.cComment,
                'cprice': this.dataForm.cPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
